<?php

class EmailController extends Zend_Controller_Action
{
    private  $auth;
    public function init()
    {
        $this->auth=  Zend_Auth::getInstance();
    }

    public function indexAction()
    {
        // action body
    }

     public function userAction()
    {
        $this->view->title="Send Email - Welcome to Rosemoore Lake";
        try{
                 if(isset($_POST['action'])){

                 require_once "Mail.php";

                 require_once "Mail/mime.php";

                $config = Zend_Registry::get('mail_config');


                 $from = $this->auth->getStorage()->read()->FIRST_NAME." " .$this->auth->getStorage()->read()->LAST_NAME ."<".$this->auth->getStorage()->read()->EMAIL_ADDRESS.">";
                 $to = "nirensinha@gmail.com";//$this->config->mail->to;
                 $subject = $_POST['subject'];
                 $body =  $_POST['emailBody'];

                 $name_of_uploaded_file =  basename($_FILES['fileName']['name']);


                 $size_of_uploaded_file = $_FILES["fileName"]["size"]/1024;

                 if($name_of_uploaded_file !="" && $size_of_uploaded_file ==0){
                     $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'>Invalid file format.</font></center>";
                }elseif($size_of_uploaded_file > $config->mail->maxsize){
                     $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'>The maximum size of file that can  be uploaded is 2 MB</font></center>";
                  }else{

                         $host = $config->mail->host;
                         $username = $config->mail->username;
                         $password =$config->mail->password;
                         $port =$config->mail->port;


                         if($size_of_uploaded_file !=0){
                                $path_of_uploaded_file = $config->mail->tempfolder . $name_of_uploaded_file;
                                $tmp_path = $_FILES["fileName"]["tmp_name"];

                                if(is_uploaded_file($tmp_path))
                                    {
                                      if(!copy($tmp_path,$path_of_uploaded_file))
                                      {
                                              $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to send the Message.</font></center>";
                                      }
                                    }
                         }

                        $message = new Mail_mime();
                        $message->setTXTBody($body);
                          if($size_of_uploaded_file !=0){
                                 $message->addAttachment($path_of_uploaded_file);
                          }
                        $body = $message->get();
                        $extraheaders = array("From"=>$from, "Subject"=>$subject,"To"=>$to);
                        $headers = $message->headers($extraheaders);
                        $mail = Mail::factory("mail");


                         $smtp = Mail::factory('smtp',
                           array ('host' => $host,
                             'auth' => true,
                             'port'=>$port,
                             'username' => $username,
                             'password' => $password));

                         $mail = $smtp->send($to, $headers, $body);

                         if($size_of_uploaded_file !=0){
                            unlink($path_of_uploaded_file);
                         }

                         if (PEAR::isError($mail)) {
                           $this->view->message ="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to send the Message.</font></center>";
                          } else {
                            $this->view->message ="<center><img src='../images/check.png'/>&nbsp;&nbsp;&nbsp<font color='green'> Message successfully sent! </font></center>";
                            }
                        }
                 }
        }catch(Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             $this->view->message="<img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp<font color='red'> Technical difficulties expierenced in sending Email</font>";
        }
    }

    public function adminAction()
    {
        $this->view->title="Send Email - Welcome to Rosemoore Lake";
        try{
              if(isset($_POST['action'])){
                   require_once "Mail.php";
                   require_once "Mail/mime.php";

                   $to="";

                   $config = Zend_Registry::get('mail_config');

                  $userMapper = new Application_Model_UserMapper();
                  
                  $result = $userMapper->getPrimaryEmail();

                  foreach ($result as $data) {
                      $to  = $to . $data->EMAIL_ADDRESS .",";
                  }

                  if($_POST["secondary"] == "Y"){
                    $result = $userMapper->getSecondaryEmail();
                            foreach ($result as $data) {
                              $to  = $to . $data->EMAIL_2 .",";
                          }
                  }

                $to = substr($to,0,strlen($to)-1);

                 $from =$config->mail->sender;

                 $subject = $_POST['subject'];
                 $body =  $_POST['emailBody'];

                 $name_of_uploaded_file =  basename($_FILES['fileName']['name']);


                 $size_of_uploaded_file = $_FILES["fileName"]["size"]/1024;

                 if($name_of_uploaded_file !="" && $size_of_uploaded_file ==0){
                     $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'>Invalid file format.</font></center>";
                }elseif($size_of_uploaded_file > $config->mail->maxsize){
                     $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'>The maximum size of file that can  be uploaded is 2 MB</font></center>";
                  }else{

                         $host = $config->mail->host;
                         $username = $config->mail->username;
                         $password =$config->mail->password;
                         $port =$config->mail->port;


                         if($size_of_uploaded_file !=0){
                                $path_of_uploaded_file = $config->mail->tempfolder . $name_of_uploaded_file;
                                $tmp_path = $_FILES["fileName"]["tmp_name"];

                                if(is_uploaded_file($tmp_path))
                                    {
                                      if(!copy($tmp_path,$path_of_uploaded_file))
                                      {
                                              $this->view->message="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to send the Message.</font></center>";
                                      }
                                    }
                         }

                        $message = new Mail_mime();
                        $message->setTXTBody($body);
                          if($size_of_uploaded_file !=0){
                                 $message->addAttachment($path_of_uploaded_file);
                          }
                        $body = $message->get();
                        $extraheaders = array("From"=>$from, "Subject"=>$subject,"To"=>$to);
                        $headers = $message->headers($extraheaders);

                   
                        $mail = Mail::factory("mail");


                         $smtp = Mail::factory('smtp',
                           array ('host' => $host,
                             'auth' => true,
                             'port'=>$port,
                             'username' => $username,
                             'password' => $password));

                         $mail = $smtp->send($to, $headers, $body);
                         
                         if($size_of_uploaded_file !=0){
                            unlink($path_of_uploaded_file);
                         }

                         if (PEAR::isError($mail)) {
                           $this->view->message ="<center><img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp;<font color='red'> Failed to send the Message.</font></center>";
                          } else {
                            $this->view->message ="<center><img src='../images/check.png'/>&nbsp;&nbsp;&nbsp<font color='green'> Message successfully sent! </font></center>";
                            }
                        }
                 }
        }catch(Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             $this->view->message="<img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp<font color='red'> Technical difficulties expierenced in sending Email</font>";
        }
    }

}

